import 'dart:developer';
import 'dart:io';

import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

///@author:create by BZY
///Date: 2021/9/7 14:09
///Description: 视频播放页面
class VideoPlayerLogic extends GetxController {
  VideoPlayerController? controller;
  String? url;
  String? path;
  String? tag;
  bool isNetwork = false;

  @override
  void onInit() {
    tag = Get.arguments?['tag'];
    url = Get.arguments?['url'];
    path=Get.arguments?['path'];
    log('videoUrl----$url');
    isNetwork = Get.arguments?['isNetwork'];
    if (isNetwork&&Platform.isIOS) {
      log('network---$url');
      controller = VideoPlayerController.network(url ?? '')
        ..initialize().then((value){
          log('netinit---');
          update();
        });
    } else {
      log('local---$path');
      controller = VideoPlayerController.asset(url ?? '')
        ..initialize().then((value) {
          log('loacalinit---');
          update();
        });
    }
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    controller?.play();
  }

  @override
  void onClose() {
    controller?.dispose();
    super.onClose();
  }

  void toggleState() {
    if(controller!.value.isPlaying){
      controller!.pause();
    }else{
      controller!.play();
    }
    update(['play']);
  }
}
